﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LinkCard.AdminCP.Common;
using LinkCard.Dao;
using LinkCard.Model;


namespace LinkCard.AdminCP.Reports
{
    public partial class report_by_customer : System.Web.UI.Page
    {
        public string sFromDate = "";
        public string sToDate = "";
        public string sCardNumber = "";
        public DateTime dFromDate = new DateTime(1900, 1, 1);
        public DateTime dToDate = new DateTime(2099, 1, 1);

        private int _totalRecords;

        private int nPageSize = 40;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                LoadPayment();
        }
        private void LoadPayment()
        {
            var db = new TopLinkEntities();

            
            int curpage = 0;
            int skip = 0;

            if (Request.QueryString["page"] != null)
            {
                if (Request.QueryString["page"].ToString() != "1")
                {
                    curpage = Convert.ToInt32(Request.QueryString["page"]);
                    skip = curpage * nPageSize;
                }
                else
                {
                    curpage = 1;
                    skip = 0;
                }
            }
            else
            {
                curpage = 1;
                skip = 0;
            }

            System.Globalization.DateTimeFormatInfo dateInfo = new System.Globalization.DateTimeFormatInfo();
            dateInfo.ShortDatePattern = "MM/dd/yyyy";

            if (sFromDate != "")
            {
                sFromDate += " 00:00:00";
                dFromDate = Convert.ToDateTime(sFromDate, dateInfo);

            }
            if (sToDate != "")
            {
                sToDate += " 23:59:59";
                dToDate = Convert.ToDateTime(sToDate, dateInfo);
            }

            var _paylog = (from payment in db.PayLog
                           from customer in db.Customers
                           //where(t=> t.CustomerID == payment.CustomerID && payment.SupplierID == nSupplierID).ToList();
                           where (customer.CustomerID == payment.CustomerID
                                 //&& payment.SupplierID == nSupplierID
                                 && payment.PayDate >= dFromDate
                                 && payment.PayDate <= dToDate
                                 && customer.CardNumber.Contains(sCardNumber))
                           select new
                           {
                               customer.CustomerID,
                               customer.CardNumber,
                               customer.Fullname,
                               payment.PayID,
                               payment.PayDate,
                               payment.PayAmount,
                               payment.PayMethod
                           }).OrderBy(t => t.PayDate).Skip(skip).Take(nPageSize);

            var paylog = _paylog.ToList();

            if (paylog.Count > 0)
            {
                _totalRecords = paylog.Count;

                int nPageCount;

                if (_totalRecords % nPageSize == 0)
                {
                    nPageCount = _totalRecords / nPageSize;
                }
                else
                {
                    nPageCount = Convert.ToInt32(_totalRecords / nPageSize) + 1;
                }

                if (nPageCount < 2) divPager.Visible = false;

                PagerShow.Text = LinkCard.Utils.hash1way.ShowPageNavigation(curpage, nPageCount);

                msgList.DataSource = paylog;

                msgList.DataBind();
                msgNoData.Visible = false;
            }
            else
            {

                msgList.DataSource = null;
                msgList.DataSourceID = null;
                msgList.DataBind();
                msgNoData.Visible = true;
            }

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            sFromDate = FromDate.Value.Trim();
            sToDate = ToDate.Value.Trim();
            sCardNumber = txtCardNumber.Value.Trim();
            LoadPayment();
        }

        protected void btnExportExcel_Click(object sender, EventArgs e)
        {
            Response.Redirect("export_by_customer.aspx?FromDate=" + sFromDate + "&ToDate=" + sToDate + "&CardNumber="+ sCardNumber + "");
        }
    }
}